package com.yj_app.qualitytest.fragment;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatSpinner;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.hu_sir.sweet_dialog_reversion.SweetAlertDialog;
import com.rengwuxian.materialedittext.MaterialAutoCompleteTextView;
import com.rengwuxian.materialedittext.MaterialEditText;
import com.yj_app.qualitytest.R;
import com.yj_app.qualitytest.adapter.ArrayAdapter;
import com.yj_app.qualitytest.adapter.BzLxAdapter;
import com.yj_app.qualitytest.adapter.CheckContentAdapter;
import com.yj_app.qualitytest.adapter.CheckXmAdapter;
import com.yj_app.qualitytest.base.BaseFragment;
import com.yj_app.qualitytest.common.Config;
import com.yj_app.qualitytest.info.HeadBoyInfo;
import com.yj_app.qualitytest.info.QLineInfo;
import com.yj_app.qualitytest.info.QScoreInfo;
import com.yj_app.qualitytest.info.QStandardInfo;
import com.yj_app.qualitytest.info.QrelationshipInfo;
import com.yj_app.qualitytest.info.SubScoreInfo;
import com.yj_app.qualitytest.info.TeamInfo;
import com.yj_app.qualitytest.info.WorkSpaceInfo;
import com.yj_app.qualitytest.util.DateUtils;
import com.yj_app.qualitytest.util.HappySQL;

import org.litepal.LitePal;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class FacadeFragment extends BaseFragment {


    @BindView(R.id.my_recy)
    RecyclerView myRecy;
    @BindView(R.id.my_recy_right)
    RecyclerView myRecyRight;
    Bundle bundle;
    @BindView(R.id.check_bzlx)
    AppCompatSpinner checkbzlx;
    List<QStandardInfo> checkbzlxList;//检查的项目

    CheckXmAdapter checkXmAdapter;//左侧栏 检查项目的适配器
    List<QStandardInfo> checkXmList = new ArrayList<>();//左侧栏 检查项目的数据源
    int checkxmSelectPos = 0;

    CheckContentAdapter checkContentAdapter;//右侧项目内容适配器
    List<QScoreInfo> checkContentList = new ArrayList<>();//检查内容的数据源
    List<QLineInfo> qLineInfoList = new ArrayList<>();//生产线数据源
    @BindView(R.id.work_shop_name)
    TextView workShopName;
    ArrayAdapter<QLineInfo> searchAdapter;

    List<HeadBoyInfo> headBoyInfos = new ArrayList<>();//主任数据源
    ArrayAdapter<HeadBoyInfo> searchHeadboyAdapter;//主任适配器
    HeadBoyInfo headBoyInfo = new HeadBoyInfo();

    @BindView(R.id.sc_line)
    MaterialAutoCompleteTextView scLine;
    @BindView(R.id.zr_search)
    MaterialAutoCompleteTextView zrSearch;
    @BindView(R.id.machine_no)
    AppCompatSpinner machineNo;
    @BindView(R.id.bz_team)
    AppCompatSpinner bzTeam;

    WorkSpaceInfo workSpaceInfo;//车间信息
    @BindView(R.id.bzfs_spinner)
    AppCompatSpinner bzfsSpinner;
    QLineInfo qLineInfo;//选中的生产线名称
    @BindView(R.id.score_value)
    TextView scoreValue;
    @BindView(R.id.rating_score)
    TextView ratingScore;


    boolean isFirstSelectMachineNo = true;
    boolean isFirstSelectTeam = true;
    boolean isFirstSelectBzfs = true;

    TeamInfo teamInfo;//已选择的班组
    int teamSelectPos;//进入页面后班组初始化的pos

    public static FacadeFragment getIntance() {
        FacadeFragment facadeFragment = new FacadeFragment();
        return facadeFragment;
    }

    public FacadeFragment setBundle(Bundle bundle) {
        this.bundle = bundle;
        return this;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (rootView == null) {
            rootView = View.inflate(getContext(), R.layout.fragment_facade, null);
        }
        ButterKnife.bind(this, rootView);
        initView();
        initEvent();
        return rootView;
    }

    private void initEvent() {
        checkContentAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                if (qLineInfo == null) {
                    baseActivity.toastError("请选择生产线后评分");
                    return;
                }

                DecimalFormat decimalFormat = new DecimalFormat("###0.#");
                QScoreInfo qScoreInfo = (QScoreInfo) adapter.getItem(position);
                View view1 = View.inflate(getContext(), R.layout.dialog_score, null);
                MaterialEditText kScore = view1.findViewById(R.id.k_sorce);
                ImageView btnSub = view1.findViewById(R.id.btn_sub);
                ImageView btnAdd = view1.findViewById(R.id.btn_add);
                kScore.setText(String.valueOf(decimalFormat.format(qScoreInfo.getScore())));
                btnSub.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (qScoreInfo.getScore() != 0) {
                            if (qScoreInfo.getScore() >= Double.parseDouble(qScoreInfo.getPoints())) {
                                //判断是否大于标准扣分
                                qScoreInfo.setScore(qScoreInfo.getScore() - Double.parseDouble(qScoreInfo.getPoints()));
                            } else {
                                qScoreInfo.setScore(0);
                            }
                        }
                        kScore.setText(decimalFormat.format(qScoreInfo.getScore()));

                    }
                });
                btnAdd.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        qScoreInfo.setScore(qScoreInfo.getScore() + Double.parseDouble(qScoreInfo.getPoints()));
                        kScore.setText(qScoreInfo.getScore() == 0 ? qScoreInfo.getPoints() : String.valueOf(decimalFormat.format(qScoreInfo.getScore())));
                    }
                });


                new SweetAlertDialog(getContext(), SweetAlertDialog.CONTENTVIEW_TYPE).setmContentView(view1)
                        .setTitleText("评分").setContentText(qScoreInfo.getItemname()).setConfirmText("确定").setCancelText("取消")
                        .setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
                            @Override
                            public void onClick(SweetAlertDialog sweetAlertDialog) {

                                qScoreInfo.setScore(Double.parseDouble(kScore.getText().toString()));
                                checkContentAdapter.notifyItemChanged(position);
                                sweetAlertDialog.dismiss();
                                //获取包装类型
                                QStandardInfo qSxm = checkXmAdapter.getItem(checkxmSelectPos);
                                QStandardInfo qSbzlx = checkbzlxList.get(checkbzlx.getSelectedItemPosition());

                                saveScore(qScoreInfo, "外观", qSbzlx, workSpaceInfo, qLineInfo, headBoyInfo, machineNo.getSelectedItem().toString(),
                                        (TeamInfo) bzTeam.getSelectedItem(), bzfsSpinner.getSelectedItem().toString(), qSxm, kScore.getText().toString());
                            }
                        }).setCancelClickListener(new SweetAlertDialog.OnSweetClickListener() {
                    @Override
                    public void onClick(SweetAlertDialog sweetAlertDialog) {
                        sweetAlertDialog.dismiss();
                    }
                }).show();

            }
        });

        /*生产线*/
        scLine.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                qLineInfo = (QLineInfo) adapterView.getItemAtPosition(i);
                workSpaceInfo = LitePal.where("cjcode = ?", qLineInfo.getCjcode()).findFirst(WorkSpaceInfo.class);
                workShopName.setText(workSpaceInfo != null ? workSpaceInfo.getCjname() : "无法获取");
                if (workSpaceInfo != null) {
                    //班组和车间不为空
                    //设置主任
                    if (workSpaceInfo != null && bzTeam.getSelectedItem() != null) {
                        TeamInfo teamInfo = (TeamInfo) bzTeam.getSelectedItem();
                        QrelationshipInfo qrelationshipInfo = LitePal.where("cjcode = ? and cteam = ?", workSpaceInfo.getCjcode(), teamInfo.getGname()).findFirst(QrelationshipInfo.class);
                        if (qrelationshipInfo == null) {
                            qrelationshipInfo = LitePal.where("cjcode = ? and cteam = ?", qLineInfo.getScxcode(), teamInfo.getGname()).findFirst(QrelationshipInfo.class);
                        }
                        if (qrelationshipInfo != null) {
                            headBoyInfo.setNickname(qrelationshipInfo.getNICKNAME());
                            headBoyInfo.setUsercode(qrelationshipInfo.getUSERCODE());
                            zrSearch.setText(qrelationshipInfo.getNICKNAME());
                        }
//                        getCheckContentList();
                        getCheckContentList2(null, "外观", checkbzlxList.get(checkbzlx.getSelectedItemPosition()), workSpaceInfo, qLineInfo, headBoyInfo, machineNo.getSelectedItem().toString(),
                                (TeamInfo) bzTeam.getSelectedItem(), bzfsSpinner.getSelectedItem().toString(), checkXmList.get(checkxmSelectPos), "");
                        checkContentAdapter.setNewData(checkContentList);
//                        initScore();
                        initScoreBySql(null, "外观", checkbzlxList.get(checkbzlx.getSelectedItemPosition()), workSpaceInfo, qLineInfo, headBoyInfo, machineNo.getSelectedItem().toString(),
                                (TeamInfo) bzTeam.getSelectedItem(), bzfsSpinner.getSelectedItem().toString(), checkXmList.get(checkxmSelectPos), "");
                    }
                }


            }
        });
        /*主任搜索的适配器*/
        zrSearch.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                headBoyInfo = (HeadBoyInfo) adapterView.getItemAtPosition(i);

                getCheckContentList2(null, "外观", checkbzlxList.get(checkbzlx.getSelectedItemPosition()), workSpaceInfo, qLineInfo, headBoyInfo, machineNo.getSelectedItem().toString(),
                        (TeamInfo) bzTeam.getSelectedItem(), bzfsSpinner.getSelectedItem().toString(), checkXmList.get(checkxmSelectPos), "");
                checkContentAdapter.setNewData(checkContentList);
//                        initScore();
                initScoreBySql(null, "外观", checkbzlxList.get(checkbzlx.getSelectedItemPosition()), workSpaceInfo, qLineInfo, headBoyInfo, machineNo.getSelectedItem().toString(),
                        (TeamInfo) bzTeam.getSelectedItem(), bzfsSpinner.getSelectedItem().toString(), checkXmList.get(checkxmSelectPos), "");
            }
        });

        checkXmAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (position == checkxmSelectPos) {
                    return;
                }
                String ccode = ((QStandardInfo) adapter.getItem(position)).getCcode();
                checkXmAdapter.setCheckxmSelectPos(checkxmSelectPos, position);
                checkxmSelectPos = position;
//                getCheckContentList();
//                checkContentAdapter.setNewData(checkContentList);
                getCheckContentList2(null, "外观", checkbzlxList.get(checkbzlx.getSelectedItemPosition()), workSpaceInfo, qLineInfo, headBoyInfo, machineNo.getSelectedItem().toString(),
                        (TeamInfo) bzTeam.getSelectedItem(), bzfsSpinner.getSelectedItem().toString(), checkXmList.get(checkxmSelectPos), "");
                checkContentAdapter.setNewData(checkContentList);
            }
        });


        checkbzlx.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                String ccode = checkbzlxList.get(i).getCcode();
                getCheckXmList(ccode);
                checkXmAdapter.setNewData(checkXmList);
                checkxmSelectPos = 0;
                checkXmAdapter.restSelectPos();
                checkContentList.clear();
                if (checkXmList.size() != 0) {
//                    getCheckContentList();
                    getCheckContentList2(null, "外观", checkbzlxList.get(checkbzlx.getSelectedItemPosition()), workSpaceInfo, qLineInfo, headBoyInfo, machineNo.getSelectedItem().toString(),
                            (TeamInfo) bzTeam.getSelectedItem(), bzfsSpinner.getSelectedItem().toString(), checkXmList.get(checkxmSelectPos), "");

                }
                checkContentAdapter.setNewData(checkContentList);
//                initScore();
                initScoreBySql(null, "外观", checkbzlxList.get(checkbzlx.getSelectedItemPosition()), workSpaceInfo, qLineInfo, headBoyInfo, machineNo.getSelectedItem().toString(),
                        (TeamInfo) bzTeam.getSelectedItem(), bzfsSpinner.getSelectedItem().toString(), checkXmList.get(checkxmSelectPos), "");
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
        /*机号选择*/
        machineNo.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                if (isFirstSelectMachineNo) {
                    isFirstSelectMachineNo = false;
                    return;
                } else {
//                    getCheckContentList();
//                    checkContentAdapter.setNewData(checkContentList);
//                    initScore();
                    getCheckContentList2(null, "外观", checkbzlxList.get(checkbzlx.getSelectedItemPosition()), workSpaceInfo, qLineInfo, headBoyInfo, machineNo.getSelectedItem().toString(),
                            (TeamInfo) bzTeam.getSelectedItem(), bzfsSpinner.getSelectedItem().toString(), checkXmList.get(checkxmSelectPos), "");
                    checkContentAdapter.setNewData(checkContentList);
//                        initScore();
                    initScoreBySql(null, "外观", checkbzlxList.get(checkbzlx.getSelectedItemPosition()), workSpaceInfo, qLineInfo, headBoyInfo, machineNo.getSelectedItem().toString(),
                            (TeamInfo) bzTeam.getSelectedItem(), bzfsSpinner.getSelectedItem().toString(), checkXmList.get(checkxmSelectPos), "");
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        /*班组选择*/
        bzTeam.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                if (isFirstSelectTeam) {
                    isFirstSelectTeam = false;
                    return;
                } else {
//                    getCheckContentList();
//                    checkContentAdapter.setNewData(checkContentList);
//                    initScore();
                    getCheckContentList2(null, "外观", checkbzlxList.get(checkbzlx.getSelectedItemPosition()), workSpaceInfo, qLineInfo, headBoyInfo, machineNo.getSelectedItem().toString(),
                            (TeamInfo) bzTeam.getSelectedItem(), bzfsSpinner.getSelectedItem().toString(), checkXmList.get(checkxmSelectPos), "");
                    checkContentAdapter.setNewData(checkContentList);
//                        initScore();
                    initScoreBySql(null, "外观", checkbzlxList.get(checkbzlx.getSelectedItemPosition()), workSpaceInfo, qLineInfo, headBoyInfo, machineNo.getSelectedItem().toString(),
                            (TeamInfo) bzTeam.getSelectedItem(), bzfsSpinner.getSelectedItem().toString(), checkXmList.get(checkxmSelectPos), "");
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        /*包装方式的spinner*/
        bzfsSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                if (isFirstSelectBzfs) {
                    isFirstSelectBzfs = false;
                    return;
                } else {
//                    getCheckContentList();
//                    checkContentAdapter.setNewData(checkContentList);
//                    initScore();
                    getCheckContentList2(null, "外观", checkbzlxList.get(checkbzlx.getSelectedItemPosition()), workSpaceInfo, qLineInfo, headBoyInfo, machineNo.getSelectedItem().toString(),
                            (TeamInfo) bzTeam.getSelectedItem(), bzfsSpinner.getSelectedItem().toString(), checkXmList.get(checkxmSelectPos), "");
                    checkContentAdapter.setNewData(checkContentList);
//                        initScore();
                    initScoreBySql(null, "外观", checkbzlxList.get(checkbzlx.getSelectedItemPosition()), workSpaceInfo, qLineInfo, headBoyInfo, machineNo.getSelectedItem().toString(),
                            (TeamInfo) bzTeam.getSelectedItem(), bzfsSpinner.getSelectedItem().toString(), checkXmList.get(checkxmSelectPos), "");
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

    }

    /**
     * @param qScoreInfo    评分的一条的bean
     * @param pName         当前页面的名称
     * @param qbzlxinfo     上方 spinner切换的包装类型
     * @param workSpaceInfo 车间bean
     * @param qLineInfo     生产线的bean
     * @param headBoyInfo   车间主任
     * @param machineNo     机号
     * @param teamInfo      班组
     * @param bzfs          包装方式
     * @param lefXm         检查左侧的项目 切换后联动内容
     */
    private void saveScore(QScoreInfo qScoreInfo, String pName, QStandardInfo qbzlxinfo,
                           WorkSpaceInfo workSpaceInfo, QLineInfo qLineInfo, HeadBoyInfo headBoyInfo, String machineNo
            , TeamInfo teamInfo, String bzfs, QStandardInfo lefXm, String kscore) {


        //扣分项目 包装方式
        String[] s = {"contentId = ? and checkxmpname=? and bzlxid = ? and bzlxname =  ? and workspacename=? and " +
                "workspaceid = ? and sclinename = ? and sclineid = ? and zrname =? and zrid =? and machineno = ? " +
                "and teamname =? and bzfs =? and checkxmid = ? and checkxmname =?",
                String.valueOf(qScoreInfo.getItemid()),
                pName,
                (qbzlxinfo == null ? "" : String.valueOf(qbzlxinfo.getCcode())),
                (qbzlxinfo == null ? "" : String.valueOf(qbzlxinfo.getCname())),
                (workSpaceInfo == null ? "" : String.valueOf(workSpaceInfo.getCjname())),
                (workSpaceInfo == null ? "" : String.valueOf(workSpaceInfo.getCjcode())),
                (qLineInfo == null ? "" : qLineInfo.getScxname()),
                (qLineInfo == null ? "" : String.valueOf(qLineInfo.getScxcode())),
                (headBoyInfo == null ? "" : headBoyInfo.getNickname()),
                (headBoyInfo == null ? "" : headBoyInfo.getUsercode()),
                machineNo,
                teamInfo.getGname(),
                bzfs,
                (lefXm == null ? "" : String.valueOf(lefXm.getCcode())),
                (lefXm == null ? "" : lefXm.getCname())};
        SubScoreInfo subScoreInfo1 = LitePal.where(s).findFirst(SubScoreInfo.class);
        if (subScoreInfo1 == null) {
            SubScoreInfo subScoreInfo = new SubScoreInfo();
            subScoreInfo.setScore(Double.parseDouble(kscore));
            subScoreInfo.setContentId(qScoreInfo.getItemid());
            subScoreInfo.setBzfs(bzfs);
            subScoreInfo.setCheckxmpname(pName);
            //
            if (null != lefXm) {
                subScoreInfo.setCheckXmId(lefXm.getCcode());
                subScoreInfo.setCheckXmName(lefXm.getCname());
            }
            if (null != qbzlxinfo) {
                subScoreInfo.setBzLxId(qbzlxinfo.getCcode());
                subScoreInfo.setBzLxName(qbzlxinfo.getCname());
            }

            //车间
            if (null != workSpaceInfo) {
                subScoreInfo.setWorkspaceid(workSpaceInfo.getCjcode());
                subScoreInfo.setWorkspacename(workSpaceInfo.getCjname());
            }
            //生产线
            if (null != qLineInfo) {

                subScoreInfo.setSclinename(qLineInfo.getScxname());
                subScoreInfo.setSclineid(String.valueOf(qLineInfo.getScxcode()));
            }
            //主任
            if (null != headBoyInfo) {

                subScoreInfo.setZrname(headBoyInfo.getNickname());
                subScoreInfo.setZrid(headBoyInfo.getUsercode());
            }
            //机号
            subScoreInfo.setMachineno(machineNo);
            subScoreInfo.setTeamname(null == teamInfo ? "" : teamInfo.getGname());
            subScoreInfo.setTime(DateUtils.convertToString(System.currentTimeMillis(),DateUtils.TIME_FORMAT));
            subScoreInfo.save();

        } else {
            subScoreInfo1.setScore(Double.parseDouble(kscore));
            subScoreInfo1.setTime(DateUtils.convertToString(System.currentTimeMillis(),DateUtils.TIME_FORMAT));
            subScoreInfo1.update(subScoreInfo1.getId());
        }


        initScoreBySql(qScoreInfo, pName, qbzlxinfo,
                workSpaceInfo, qLineInfo, headBoyInfo, machineNo
                , teamInfo, bzfs, lefXm, kscore);


    }

    private void initScoreBySql(QScoreInfo qScoreInfo, String pName, QStandardInfo qbzlxinfo,
                                WorkSpaceInfo workSpaceInfo, QLineInfo qLineInfo, HeadBoyInfo headBoyInfo, String machineNo
            , TeamInfo teamInfo, String bzfs, QStandardInfo lefXm, String kscore) {
        double count = 0;
        if (workSpaceInfo == null || qLineInfo == null || headBoyInfo == null) {
            scoreValue.setText(baseActivity.formatDouble(count));
        } else {
            String sql = "SELECT sum(score) FROM subscoreinfo j1 WHERE j1.checkxmpname= '" + pName + "' " +
                    "AND j1.bzlxid= '" + (qbzlxinfo == null ? "" : String.valueOf(qbzlxinfo.getCcode())) + "' " +
                    "AND j1.bzlxname= '" + (qbzlxinfo == null ? "" : String.valueOf(qbzlxinfo.getCname())) + "' " +
                    "AND j1.workspacename= '" + (workSpaceInfo == null ? "" : String.valueOf(workSpaceInfo.getCjname())) + "' " +
                    "AND j1.workspaceid= '" + (workSpaceInfo == null ? "" : String.valueOf(workSpaceInfo.getCjcode())) + "' " +
                    "AND j1.sclinename= '" + (qLineInfo == null ? "" : qLineInfo.getScxname()) + "' " +
                    "AND j1.sclineid= '" + (qLineInfo == null ? "" : String.valueOf(qLineInfo.getScxcode())) + "' " +
                    "AND j1.zrname= '" + (headBoyInfo == null ? "" : headBoyInfo.getNickname()) + "' " +
                    "AND j1.zrid= '" + (headBoyInfo == null ? "" : headBoyInfo.getUsercode()) + "' " +
                    "AND j1.machineno= '" + machineNo + "' " +
                    "AND j1.teamname= '" + (null == teamInfo ? "" : teamInfo.getGname()) + "' " +
                    "AND j1.bzfs= '" + bzfs + "' ";
            Cursor cursor = LitePal.findBySQL(sql);
            if (cursor.getCount() != 0 && cursor.moveToFirst()) {
                count = cursor.getDouble(cursor.getColumnIndex("sum(score)"));
                scoreValue.setText(baseActivity.formatDouble(count));
            }
            cursor.close();
        }
    }

    private void getCheckContentList2(QScoreInfo qScoreInfo, String pName, QStandardInfo qbzlxinfo,
                                      WorkSpaceInfo workSpaceInfo, QLineInfo qLineInfo, HeadBoyInfo headBoyInfo, String machineNo
            , TeamInfo teamInfo, String bzfs, QStandardInfo lefXm, String kscore) {

        if (workSpaceInfo == null || qLineInfo == null || headBoyInfo == null) {
            checkContentList = LitePal.where("ccode = ?", lefXm.getCcode()).order("itemid").find(QScoreInfo.class);
        } else {
            String sql = "SELECT c.id,c.ccode,c.csealflag,c.itemname,c.itemid,c.itemmemo,c.points ,j1.id suid, j1.score\n" +
                    "FROM qscoreinfo c\n" +
                    "LEFT JOIN subscoreinfo j1 ON c.itemid = j1.contentid and j1.checkxmpname= '"+pName+"' AND j1.bzlxid= '" +
                    (qbzlxinfo == null ? "" : String.valueOf(qbzlxinfo.getCcode()))  + "' " +
                    "AND j1.bzlxname= '" + (qbzlxinfo == null ? "" : String.valueOf(qbzlxinfo.getCname())) + "' " +
                    "AND j1.checkxmid= '" + (lefXm == null ? "" : String.valueOf(lefXm.getCcode())) + "' " +
                    "AND j1.checkxmname= '" + (lefXm == null ? "" : String.valueOf(lefXm.getCname())) + "' " +
                    "AND j1.workspacename= '" + (workSpaceInfo == null ? "" : String.valueOf(workSpaceInfo.getCjname()))  + "' " +
                    "AND j1.workspaceid= '" +(workSpaceInfo == null ? "" : String.valueOf(workSpaceInfo.getCjcode())) + "' " +
                    "AND j1.sclinename= '" + (qLineInfo == null ? "" : qLineInfo.getScxname()) + "' " +
                    "AND j1.sclineid= '" + (qLineInfo == null ? "" : String.valueOf(qLineInfo.getScxcode()))+ "' " +
                    "AND j1.zrname= '" + (headBoyInfo == null ? "" : headBoyInfo.getNickname()) + "' " +
                    "AND j1.zrid= '" + (headBoyInfo == null ? "" : headBoyInfo.getUsercode())+ "' " +
                    "AND j1.machineno= '" + machineNo + "' " +
                    "AND j1.teamname= '" + (null == teamInfo ? "" : teamInfo.getGname()) + "' " +
                    "AND j1.bzfs= '" + bzfs + "' " + "WHERE  c.ccode = '" + (lefXm == null ? "" : String.valueOf(lefXm.getCcode())) + "'";
            try {
                Cursor cursor = LitePal.findBySQL(sql);
                checkContentList = HappySQL.cursor2VOList(cursor, QScoreInfo.class);
                if (checkContentList == null && checkContentList.size() == 0) {
                    checkContentList = LitePal.where("ccode = ?", lefXm.getCcode()).order("itemid").find(QScoreInfo.class);
                }

            } catch (Exception e) {
                checkContentList = LitePal.where("ccode = ?", lefXm.getCcode()).order("itemid").find(QScoreInfo.class);
            }
        }
    }

    private void getCheckContentList() {
        //获取选中的包装类型
        QStandardInfo getSelectBzLx = checkbzlxList.get(checkbzlx.getSelectedItemPosition());
        //获取左侧栏选中的项目
        QStandardInfo checkXm = checkXmList.get(checkxmSelectPos);
        //获取车间
//        workSpaceInfo;
        //获取生产线
//        qLineInfo;
        //获取主任的信息
//        headBoyInfo;
        //机器号码
        String machineno = machineNo.getSelectedItem().toString();
        //班组信息
        String teamname = bzTeam.getSelectedItem().toString();
        //包装方式
        String bzfs = bzfsSpinner.getSelectedItem().toString();
        if (workSpaceInfo == null || qLineInfo == null || headBoyInfo == null) {
            checkContentList = LitePal.where("ccode = ?", checkXm.getCcode()).order("itemid").find(QScoreInfo.class);
        } else {
            String sql = "SELECT c.id,c.ccode,c.csealflag,c.itemname,c.itemid,c.itemmemo,c.points ,j1.id suid, j1.score\n" +
                    "FROM qscoreinfo c\n" +
                    "LEFT JOIN subscoreinfo j1 ON c.id = j1.contentid and j1.checkxmpname= '外观' AND j1.bzlxid= '" + getSelectBzLx.getCcode() + "' " +
                    "AND j1.bzlxname= '" + getSelectBzLx.getCname() + "' " +
                    "AND j1.checkxmid= '" + checkXm.getCcode() + "' " +
                    "AND j1.checkxmname= '" + checkXm.getCname() + "' " +
                    "AND j1.workspacename= '" + workSpaceInfo.getCjname() + "' " +
                    "AND j1.workspaceid= '" + workSpaceInfo.getCjcode() + "' " +
                    "AND j1.sclinename= '" + qLineInfo.getScxname() + "' " +
                    "AND j1.sclineid= '" + qLineInfo.getScxcode() + "' " +
                    "AND j1.zrname= '" + headBoyInfo.getNickname() + "' " +
                    "AND j1.zrid= '" + headBoyInfo.getUsercode() + "' " +
                    "AND j1.machineno= '" + machineno + "' " +
                    "AND j1.teamname= '" + teamname + "' " +
                    "AND j1.bzfs= '" + bzfs + "' " + "WHERE  c.ccode = '" + checkXm.getCcode() + "'";
            try {
                Cursor cursor = LitePal.findBySQL(sql);
                checkContentList = HappySQL.cursor2VOList(cursor, QScoreInfo.class);
                if (checkContentList == null && checkContentList.size() == 0) {
                    checkContentList = LitePal.where("ccode = ?", checkXm.getCcode()).order("itemid").find(QScoreInfo.class);
                }

            } catch (Exception e) {
                checkContentList = LitePal.where("ccode = ?", checkXm.getCcode()).order("itemid").find(QScoreInfo.class);
            }
        }
    }

    /*获取左边的数据源*/
    private void getCheckXmList(String code) {

        checkXmList =
                LitePal.where("qstandardinfo.istep = 4 AND\n" +
                        "qstandardinfo.itype = 1 AND\n" +
                        "qstandardinfo.ccode LIKE ?", code + "%").find(QStandardInfo.class);

    }


    private void initView() {
        setRecyclerViewManager(myRecy);
        setRecyclerViewManager(myRecyRight);
        initCheckXmAdapter();
        initCheckContentAdapter();
        initcheckBzlxAdapter();
        initScLineAdapter();
        initZrAdapter();
        initTeam();


    }

    /*核算分数显示*/
    private void initScore() {
        //获取选中的包装类型
        QStandardInfo getSelectBzLx = checkbzlxList.get(checkbzlx.getSelectedItemPosition());
        //获取左侧栏选中的项目
        QStandardInfo checkXm = checkXmList.get(checkxmSelectPos);
        //获取车间
//        workSpaceInfo;
        //获取生产线
//        qLineInfo;
        //获取主任的信息
//        headBoyInfo;
        //机器号码
        String machineno = machineNo.getSelectedItem().toString();
        //班组信息
        String teamname = bzTeam.getSelectedItem().toString();
        //包装方式
        String bzfs = bzfsSpinner.getSelectedItem().toString();
        double count = 0;
        if (workSpaceInfo == null || qLineInfo == null || headBoyInfo == null) {
            scoreValue.setText(baseActivity.formatDouble(count));
        } else {
            String sql = "SELECT sum(score) FROM subscoreinfo j1 WHERE j1.checkxmpname= '外观' " +
                    "AND j1.bzlxid= '" + getSelectBzLx.getCcode() + "' " +
                    "AND j1.bzlxname= '" + getSelectBzLx.getCname() + "' " +
                    "AND j1.workspacename= '" + workSpaceInfo.getCjname() + "' " +
                    "AND j1.workspaceid= '" + workSpaceInfo.getCjcode() + "' " +
                    "AND j1.sclinename= '" + qLineInfo.getScxname() + "' " +
                    "AND j1.sclineid= '" + qLineInfo.getScxcode() + "' " +
                    "AND j1.zrname= '" + headBoyInfo.getNickname() + "' " +
                    "AND j1.zrid= '" + headBoyInfo.getUsercode() + "' " +
                    "AND j1.machineno= '" + machineno + "' " +
                    "AND j1.teamname= '" + teamname + "' " +
                    "AND j1.bzfs= '" + bzfs + "' ";
            Cursor cursor = LitePal.findBySQL(sql);
            if (cursor.getCount() != 0 && cursor.moveToFirst()) {
                count = cursor.getDouble(cursor.getColumnIndex("sum(score)"));
                scoreValue.setText(baseActivity.formatDouble(count));
            }
            cursor.close();
        }


    }

    /*初始化班组*/
    private void initTeam() {
        teamInfo = (TeamInfo) bundle.getSerializable(Config.TEAM_INFO);
        teamSelectPos = bundle.getInt(Config.TEAM_POSTION);
        List<TeamInfo> teamInfoList = LitePal.order("id").find(TeamInfo.class);
        ArrayAdapter<TeamInfo> teamInfoArrayAdapter = new ArrayAdapter<TeamInfo>(getContext(), android.R.layout.simple_list_item_1, teamInfoList);
        bzTeam.setAdapter(teamInfoArrayAdapter);
        bzTeam.setSelection(teamSelectPos);

    }

    private void initZrAdapter() {
        List<HeadBoyInfo> headBoyInfos = LitePal.findAll(HeadBoyInfo.class);
        searchHeadboyAdapter = new ArrayAdapter(getContext(), R.layout.simple_list_item_1, headBoyInfos);
        zrSearch.setAdapter(searchHeadboyAdapter);
        showDropDown(zrSearch);
    }

    private void initScLineAdapter() {
        List<QLineInfo> qLineInfos = LitePal.findAll(QLineInfo.class);
        searchAdapter = new ArrayAdapter(getContext(), R.layout.simple_list_item_1, qLineInfos);
        scLine.setAdapter(searchAdapter);
        showDropDown(scLine);
    }


    /*检查内容的适配器*/
    private void initCheckContentAdapter() {
        checkContentAdapter = new CheckContentAdapter(R.layout.item_check_content, null);
        myRecyRight.setAdapter(checkContentAdapter);
    }

    /*检查项目的适配器*/
    private void initCheckXmAdapter() {
        checkXmAdapter = new CheckXmAdapter(R.layout.item_one_textview, null);
        myRecy.setAdapter(checkXmAdapter);
    }

    /*获取包装的类型 如“纸包 扎把”的数据 并装入适配器*/
    private void initcheckBzlxAdapter() {
        checkbzlxList = LitePal.where("qstandardinfo.istep = 3 AND qstandardinfo.itype = 1 AND qstandardinfo.ccode LIKE ? ", bundle.getString(Config.P_ID) + "01%").find(QStandardInfo.class);
        ArrayAdapter<QStandardInfo> arrayAdapter =new ArrayAdapter<QStandardInfo>(getContext(),R.layout.simple_list_item_1,checkbzlxList);
        checkbzlx.setAdapter(arrayAdapter);


    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
